package suncertify.ui;

//~--- non-JDK imports --------------------------------------------------------


//~--- JDK imports ------------------------------------------------------------

import java.awt.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
/**
 *
 * @author jshah
 * The class representing the main UI screen of the application, where all the records are viewed edited and searched by the User
 */
public class CSRScreen {

    /**
     * bookButton         button to bookButton the selected record in JTable
     */
    JButton             bookButton;
    /**
     * customerID         Text Input to enter the customer id of the customer
     */
    JTextField          customerID;
    /**
     *  recordsTable       Table to display the Records returned as result of search
     */
    JTable              recordsTable;
    /**
     * locationField      Text Input to enter location which needs to be matched in all Records
     */
    JTextField          locationField;
    /**
     * parentFrame        Main Frame of the UI in which contains <code>containerPane</code>
     */
    JFrame              parentFrame;
    /**
     * containerPane      The container which contains all the components in the frame
     */
    Container           containerPane;
    /**
     * parentPanel        The base Panel which contains other JPanels for other ares of UI
     */
    JPanel              parentPanel;
    /**
     * nameField          Text input to enter name which needs to be matched in all Records
     */
    JTextField          nameField;
    /**
     * searchAllButton    button to Display all Records in <code>recordsTable</code>
     */
    JButton             searchAllButton;
    /**
     * searchButton       button to Display Records which match the criteria entered in <code>nameField</code> and <code>locationField</code>
     */
    JButton             searchButton;
    /**
     * tableModel         <code>DefaultTableModel</code> acts as interface for the <code>JTable recordsTable</code>
     */
    DefaultTableModel   tableModel;
    /**
     * unbookButton       button to unbook a record selected in JTable
     */
    JButton             unbookButton;
    /**
     * refreshRates      combo box to select the time of how often the table is refreshed with current data in the dbfile
     */
    JComboBox           refreshRates;
    /**
     * refreshTable     button to refresh the table with updated data in the dbfile 
     */
    JButton             refreshTable;
    /**
     * screenController   controller which is delegated all the UI events from <code>CSRScreen</code>
     */
    CSRScreenController screenController = new CSRScreenController(this);
    /**
     * Array of different durations to show how often the table can be refreshed
     */
    static final String[] rates = new String[]{"5","10","15","20","25","30","35","40","45","50","55","60"};

    enum colNames{

         RecordNo("RecordNo.",60), Name("NAME",200), Location("LOCATION",100), Speciality("SPECIALTY",200), Size("SIZE",50), Price("PRICE/HR",80), BookedBy("BOOKED BY",100);

         private String label;
         private int    colwidth;

         colNames(String label, int colwidth){
                this.label = label;
                this.colwidth = colwidth;
         }

         String getLabel(){
             return this.label;
         }

         int getColWidth(){
             return this.colwidth;
         }
    }

    CSRScreen() {
        parentFrame = new JFrame("CSR CONSOLE");
    }

    public JButton getBook() {
        return bookButton;
    }

    public JTextField getCustomerID() {
        return customerID;
    }

    public JButton getUnbook() {
        return unbookButton;
    }

    public JTable getJtable() {
        return recordsTable;
    }

    public void setJtable(JTable jtable) {
        this.recordsTable = jtable;
    }

    public DefaultTableModel getTableModel() {
        return tableModel;
    }

    public void setTableModel(DefaultTableModel tableModel) {
        this.tableModel = tableModel;
    }

    public JTextField getLocationField() {
        return locationField;
    }

    public void setLocationField(JTextField locationField) {
        this.locationField = locationField;
    }

    public JTextField getNamField() {
        return nameField;
    }

    public void setNamField(JTextField namField) {
        this.nameField = namField;
    }

    public JButton getSearchAllButton() {
        return searchAllButton;
    }

    public void setSearchAllButton(JButton searchAllButton) {
        this.searchAllButton = searchAllButton;
    }

    public JButton getSearchButton() {
        return searchButton;
    }

    public void
            setSearchButton(JButton searchButton) {
        this.searchButton = searchButton;
    }

    public JComboBox getRefreshRates() {
        return refreshRates;
    }

    public JButton getRefreshTable() {
        return refreshTable;
    }

    
    /**
     *
     * @return        <code>JPanel</code> the parent panel which contains sub panels for table, booking area and search area
     */
     JPanel getMainPanel() {
        JPanel    p = new JPanel(new GridBagLayout());
        Dimension d = new Dimension(800, 700);

        p.setPreferredSize(d);
        p.setBorder(BorderFactory.createTitledBorder("CSR Console"));
        p.setBorder(BorderFactory.createLineBorder(Color.yellow, 2));

        GridBagConstraints c = new GridBagConstraints();

        setMyConstraints(c, 0, 0, GridBagConstraints.NORTH);
        c.gridwidth = 2;
        p.add(getTablePanel(), c);
        setMyConstraints(c, 0, 1, GridBagConstraints.NORTH);
        c.gridwidth = 1;
        p.add(getFieldPanel(), c);
        setMyConstraints(c, 1, 1, GridBagConstraints.NORTH);
        c.gridwidth = 1;
        p.add(getBookingPanel(), c);
        p.setBackground(Color.BLACK);

        return p;
    }

    /**
     * @return  <code>JPanel</code> containing the search area, Text Input for criteria and button for Search
     */
    private JPanel getFieldPanel() {
        JPanel    p = new JPanel(new GridBagLayout());
        Dimension d = new Dimension(400, 200);

        p.setPreferredSize(d);
        p.setMinimumSize(d);
        p.setBorder(BorderFactory.createTitledBorder("SEARCH CONTRACTORS"));

        GridBagConstraints c = new GridBagConstraints();

        setMyConstraints(c, 0, 0, GridBagConstraints.NORTHEAST);
        p.add(new JLabel("Name:"), c);
        setMyConstraints(c, 1, 0, GridBagConstraints.NORTHEAST);
        this.nameField = new JTextField(16);
        p.add(this.nameField, c);
        setMyConstraints(c, 0, 1, GridBagConstraints.NORTHEAST);
        p.add(new JLabel("Location:"), c);
        setMyConstraints(c, 1, 1, GridBagConstraints.NORTHEAST);
        this.locationField = new JTextField(16);
        p.add(this.locationField, c);
        setMyConstraints(c, 1, 2, GridBagConstraints.NORTHEAST);
        p.add(getButtonPanel(), c);

        return p;
    }

    /**
     * @return <code>JPanel</code> which contains Text Input for customer ID and buttons for 'book' and 'unbook'
     */
    private JPanel getBookingPanel() {
        JPanel    jpanel = new JPanel(new GridBagLayout());
        Dimension d      = new Dimension(400, 200);
        JLabel combolabel = new JLabel("Select A Refresh Rate:");

        jpanel.setPreferredSize(d);
        jpanel.setMinimumSize(d);
        jpanel.setBorder(BorderFactory.createTitledBorder("BOOKING"));
        this.customerID = new JTextField(16);
        this.customerID.addActionListener(screenController);
        this.bookButton       = new JButton("BOOK");
        this.unbookButton     = new JButton("UNBOOK");
        this.refreshRates     = new JComboBox(rates);
        this.refreshTable     = new JButton("REFRESH");

        // add listeners
        GridBagConstraints c = new GridBagConstraints();
        this.bookButton.addActionListener(screenController);
        this.unbookButton.addActionListener(screenController);
        this.refreshTable.addActionListener(screenController);
        this.refreshRates.addItemListener(screenController);

        int anchor =  GridBagConstraints.NORTHEAST;
        this.setMyConstraints(c, 0, 0, anchor);
        jpanel.add(new JLabel("4 DIGIT CUSTOMER ID:"),c);
        this.setMyConstraints(c, 1, 0,anchor);
        jpanel.add(this.customerID, c);
        this.setMyConstraints(c, 2, 0, anchor);
        jpanel.add(this.bookButton,c);
        this.setMyConstraints(c, 3, 0, anchor);
        jpanel.add(this.unbookButton,c);
        this.setMyConstraints(c, 0, 1, anchor);
        jpanel.add(combolabel,c);
        this.setMyConstraints(c, 1, 1, anchor);
        jpanel.add(this.refreshRates,c);
        this.setMyConstraints(c, 2, 1, anchor);
        jpanel.add(this.refreshTable,c);

        return jpanel;
    }

    private JPanel getButtonPanel() {
        JPanel p = new JPanel(new GridBagLayout());

        this.searchButton = new JButton("SEARCH");

        GridBagConstraints c = new GridBagConstraints();

        setMyConstraints(c, 0, 0, GridBagConstraints.NORTHEAST);
        p.add(this.searchButton, c);
        this.searchButton.addActionListener(this.screenController);
        setMyConstraints(c, 0, 1, GridBagConstraints.NORTHEAST);
        p.add(new JLabel("             OR      "), c);
        setMyConstraints(c, 0, 2, GridBagConstraints.NORTHEAST);
        this.searchAllButton = new JButton("SEARCH ALL");
        p.add(this.searchAllButton, c);
        this.searchAllButton.addActionListener(this.screenController);

        return p;
    }

    /**
     * set a <code>GridBagConstraints</code> instance to add components
     * @param c        <code>GridBagConstraints</code> instance to be modified
     * @param gridx    x-coordinate of the Grid
     * @param gridy    y-coordinate of the Grid
     * @param anchor   A constant value to specify which direction the component will remain anchored in case of change in size
     */
    private void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
        c.gridx  = gridx;
        c.gridy  = gridy;
        c.anchor = anchor;
        c.insets = new Insets(5, 5, 0, 5);
        c.fill   = GridBagConstraints.BOTH;
    }

    /**
     * Overloaded <code>setMyConstraints</code> for cases where <code>Insets</code> is needed
     * @param c
     * @param gridx
     * @param gridy
     * @param anchor
     * @param inset
     */
    private void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor, Insets inset) {
        setMyConstraints(c, gridx, gridy, anchor);
        c.insets = inset;
    }

    /**
     * @return      <code>JPanel</code> which contains the <code>JTable</code> showing the results of search
     */
    private JPanel getTablePanel() {
        JPanel             p = new JPanel(new GridBagLayout());
        Dimension          d = new Dimension(800, 400);
        GridBagConstraints c = new GridBagConstraints();

        setMyConstraints(c, 0, 0, GridBagConstraints.NORTH);
        p.setBorder(BorderFactory.createTitledBorder("CSR Table"));
        p.setPreferredSize(d);
        p.setMinimumSize(d);
        c.fill = GridBagConstraints.BOTH;
        p.add(getTable(), c);

        return p;
    }

    /**
     *
     * @return      <code>JScrollPane</code> containing the table
     */
    private JScrollPane getTable() {


        // The data used in the table, placed as a multi-dimensional array.
        this.tableModel = new DefaultTableModel();

        for (colNames c: colNames.values()){
            this.tableModel.addColumn(c.getLabel());
        }

        for (int i = 0; i < 35; i++) {
            Object[] empty = {};

            this.tableModel.addRow(empty);
        }

        recordsTable = new JTable();
        recordsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        recordsTable.setModel(tableModel);

        Dimension d = new Dimension(800, 390);

        recordsTable.setMinimumSize(d);
        recordsTable.setFillsViewportHeight(true);
        recordsTable.getSelectionModel().addListSelectionListener(screenController);
        

        JTableHeader header = recordsTable.getTableHeader();

        header.setBackground(Color.yellow);

        JScrollPane tableScroll = new JScrollPane(recordsTable);

        tableScroll.setMinimumSize(d);

       

        return tableScroll;
    }

    /**
     *Set fixed column width for each column in JTable
     * @param recordsTable the <code>JTable</code> that displays the records 
     */

    public static void setColumnWidth(JTable recordsTable){
         //set column widths

          for(int index = 0; index < recordsTable.getColumnModel().getColumnCount();index++){

              //for each table column get the corresponding column width from the table
              recordsTable.getColumnModel().getColumn(index).setMinWidth(colNames.values()[index].getColWidth());
          }
    }

    /**
     * get the handle for a  <code>Container</code> of all the components in the screen
     * @return          The root <code>Container</code> containing all the components
     */
    public Container getMyPane() {
        return containerPane;
    }
}


